package com.ruoyi.web.controller.report;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.data.MD5;
import com.ruoyi.date.DateFormater;
import com.ruoyi.fundUsage.domain.FundUsageT;
import com.ruoyi.fundUsage.service.IFundUsageTService;
import com.ruoyi.unit.service.IHouseUnitTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * 返还Controller
 * 
 * @author lijie
 * @date 2023-05-04
 */
@RestController
@RequestMapping("/report/fundUsage")
public class FundUsageTController extends BaseController
{
    @Autowired
    private IFundUsageTService fundUsageTService;

    @Autowired
    private IHouseUnitTService houseUnitTService;

    /**
     * 查询总户数，总缴费金额，总余额,总利息
     */
    @GetMapping("/getDoorSum")
    public AjaxResult getDoorSum()
    {
        FundUsageT fundUsageVO = new FundUsageT();
        FundUsageT fundUsageT = fundUsageTService.getDoorInfoSum(fundUsageVO);
        return AjaxResult.success(fundUsageT);
    }

    /**
     * 查询当日缴费笔数，当日缴费金额，当日维修金额
     */
    @GetMapping("/getCostSumToday")
    public AjaxResult getCostSumToday()
    {
        FundUsageT fundUsageVO = new FundUsageT();
        fundUsageVO.setTodayDate(DateFormater.getSystemCurrentDate());
//        fundUsageVO.setYearDate(DateFormater.getSystemCurrentDateYY());
        FundUsageT fundUsageT = fundUsageTService.getCostInfoSum(fundUsageVO);
        FundUsageT fundUsageT1 = fundUsageTService.getRepairInfoSum(fundUsageVO);
        fundUsageT.setRepairSum(fundUsageT1.getRepairSum());
        return AjaxResult.success(fundUsageT);
    }

    /**
     * 查询今年缴费笔数，今年缴费金额，今年维修笔数，今年维修金额
     */
    @GetMapping("/getCostSumYear")
    public AjaxResult getCostSumYear() throws ParseException {
        FundUsageT fundUsageVO = new FundUsageT();
        fundUsageVO.setYearDate(DateFormater.getSystemCurrentDate());
        FundUsageT fundUsageT = fundUsageTService.getCostInfoSum(fundUsageVO);
        FundUsageT fundUsageT1 = fundUsageTService.getRepairInfoSum(fundUsageVO);
        fundUsageT.setRepairSum(fundUsageT1.getRepairSum());
        fundUsageT.setRepairMoney(fundUsageT1.getRepairMoney());

        FundUsageT fundUsageVO1 = new FundUsageT();
        fundUsageVO1.setYearDate(DateFormater.formatDate(DateFormater.addYearToDate(DateFormater.strToDate1(DateFormater.getSystemCurrentDate()),-1)));
        FundUsageT fundUsageT2 = fundUsageTService.getCostInfoSum(fundUsageVO1);
        FundUsageT fundUsageT3 = fundUsageTService.getRepairInfoSum(fundUsageVO1);

        fundUsageT.setRatio1((int)((fundUsageT.getCostSum()/fundUsageT2.getCostSum()-1)*100)+"%");
        fundUsageT.setRatio2((int)((fundUsageT.getCostMoney()/fundUsageT2.getCostMoney()-1)*100)+"%");
        fundUsageT.setRatio3((int)((fundUsageT.getRepairSum()/fundUsageT3.getRepairSum()-1)*100)+"%");
        fundUsageT.setRatio4((int)((fundUsageT.getRepairMoney()/fundUsageT3.getRepairMoney()-1)*100)+"%");

        return AjaxResult.success(fundUsageT);
    }

    /**
     * 查询小区，地址，缴费时间，姓名
     */
    @GetMapping("/getDoorInfo")
    public TableDataInfo getDoorInfo() throws Exception {
        FundUsageT fundUsageVO = new FundUsageT();
        List<FundUsageT> fundUsageList = fundUsageTService.getDoorInfo(fundUsageVO);
        for(FundUsageT fundUsageT:fundUsageList){
            fundUsageT.setUserName(MD5.decrypt(fundUsageT.getUserName()));
        }
        return getDataTable(fundUsageList);
    }

    /**
     * 查询已缴户数
     */
    @GetMapping("/getDoorCountByCostSts")
    public AjaxResult getDoorCountByCostSts() throws Exception {
        FundUsageT fundUsageVO = new FundUsageT();
        fundUsageVO.setCostSts("1");
        FundUsageT fundUsageT = fundUsageTService.getDoorInfoSum(fundUsageVO);

        FundUsageT fundUsageVO1 = new FundUsageT();
        FundUsageT fundUsageT1 = fundUsageTService.getDoorInfoSum(fundUsageVO1);
        fundUsageT.setRatio5((int)(fundUsageT.getDoorSum() / fundUsageT1.getDoorSum() * 100d));
        return AjaxResult.success(fundUsageT);
    }

    /**
     * 查询未缴户数
     */
    @GetMapping("/getDoorCountByNoCostSts")
    public AjaxResult getDoorCountByNoCostSts() throws Exception {
        FundUsageT fundUsageVO = new FundUsageT();
        fundUsageVO.setCostSts("0");
        FundUsageT fundUsageT = fundUsageTService.getDoorInfoSum(fundUsageVO);
        return AjaxResult.success(fundUsageT);
    }

    /**
     * 查询各用途户数
     */
    @GetMapping("/getDoorSumByPurpose")
    public TableDataInfo getDoorSumByPurpose() throws Exception {
        FundUsageT fundUsageVO = new FundUsageT();
        List<FundUsageT> fundUsageT = fundUsageTService.getDoorInfoSumByPurpose(fundUsageVO);
        return getDataTable(fundUsageT);
    }


    /**
     * 查询总户数，总缴费金额，总余额,总利息按年度
     */
    @GetMapping("/getDoorSumByYear")
    public TableDataInfo getDoorSumByYear()
    {
        List<FundUsageT> fundUsageTList = fundUsageTService.getYearGroup();
        for(FundUsageT fundUsageVO:fundUsageTList){
            FundUsageT fundUsageT1 = fundUsageTService.getCostInfoSumByYear(fundUsageVO);
            FundUsageT fundUsageT2 = fundUsageTService.getRepairInfoSumByYear(fundUsageVO);
            FundUsageT fundUsageT3 = fundUsageTService.getRateInfoSumByYear(fundUsageVO);
            fundUsageVO.setCostMoney(fundUsageT1.getCostMoney());
            fundUsageVO.setRepairMoney(fundUsageT2.getRepairMoney());
            fundUsageVO.setRateMoney(fundUsageT3.getRateMoney());
        }

        return getDataTable(fundUsageTList);
    }


    /**
     * 查询第三方管理
     */
    @GetMapping("/getTripartiteSum")
    public AjaxResult getTripartiteSum() throws Exception {
        FundUsageT fundUsageVO = new FundUsageT();
        FundUsageT fundUsageT = fundUsageTService.getCompanyInfoSum(fundUsageVO);
        FundUsageT fundUsageT1 = fundUsageTService.getEstateInfoSum(fundUsageVO);
        FundUsageT fundUsageT2 = fundUsageTService.getStreetInfoSum(fundUsageVO);
        FundUsageT fundUsageT3 = fundUsageTService.getCommunityInfoSum(fundUsageVO);
        FundUsageT fundUsageT4 = fundUsageTService.getCommitteeInfoSum(fundUsageVO);
        FundUsageT fundUsageT5 = fundUsageTService.getPropertyInfoSum(fundUsageVO);
        fundUsageVO.setCompanySum(fundUsageT.getCompanySum());
        fundUsageVO.setEstateSum(fundUsageT1.getEstateSum());
        fundUsageVO.setStreetSum(fundUsageT2.getStreetSum());
        fundUsageVO.setCommunitySum(fundUsageT3.getCommunitySum());
        fundUsageVO.setCommitteeSum(fundUsageT4.getCommitteeSum());
        fundUsageVO.setPropertySum(fundUsageT5.getPropertySum());
        return AjaxResult.success(fundUsageVO);
    }
}
